// pages/picturebook/picturebook.js
Page({
    data: {
    //分类列表（默认选中"经典绘本"）
    categoryList: [
        "经典绘本", "国学绘本",  "数学绘本", "科学绘本",
        "其它中文绘本", "歪歪兔系列", "巴巴爸爸系列", "金奖绘本"
    ],
    // 默认选中分类    
    activeCategory: "经典绘本", 
    // 存储绘本列表数据
      bookList: [
        {
          id: 0,
          cover: "/images/picturebook/three_pig/cover_three_pigs.png", // 本地封面路径
          title: "三只小猪",
          ageRange: "3-6岁",
          tags: "侦探悬疑 推理分析",
          pageCount: 19,
          playCount: 8.62,
          category: "经典绘本", // 关联分类
          // 阅读页需要的所有页面（模拟）
          pages: [
            "/images/picturebook/three_pig/page_three_pigs_1.png",
            "/images/picturebook/three_pig/page_three_pigs_2.png",
            "/images/picturebook/three_pig/page_three_pigs_3.png",
            "/images/picturebook/three_pig/page_three_pigs_4.png",
            // ...更多页
          ]
        },
        {
            id: 1,
            cover: "/images/picturebook/secret_sea/cover_secret_sea.jpg",
            title: "海底的秘密",
            ageRange: "6-9岁",
            tags: "AI互动解谜 逻辑思维",
            pageCount: 17,
            playCount: 9.84,
            category: "经典绘本", // 关联分类
            pages: [
                "/images/picturebook/secret_sea/page_secret_sea_1.jpg",
                "/images/picturebook/secret_sea/page_secret_sea_2.jpg",
                "/images/picturebook/secret_sea/page_secret_sea_3.jpg",
                "/images/picturebook/secret_sea/page_secret_sea_4.jpg",
                // ...更多页
            ]
        },
        {
            id: 2,
            cover: "/images/picturebook/tree_good/cover_tree_good.jpg",
            title: "树真好",
            ageRange: "3-6岁",
            tags: "名著启蒙 脑筋急转弯",
            pageCount: 16,
            playCount: 1.82,
            category: "国学绘本", // 关联分类
            pages: [
                "/images/picturebook/tree_good/page_tree_good_1.jpg",
                "/images/picturebook/tree_good/page_tree_good_2.jpg",
                "/images/picturebook/tree_good/page_tree_good_3.jpg",
                "/images/picturebook/tree_good/page_tree_good_4.jpg",
                // ...更多页
            ]
        },
        {
            id: 3,
            cover: "/images/picturebook/rabbit_friend/cover_rabbit_friend.jpg",
            title: "我的兔子朋友",
            ageRange: "1-3岁",
            tags: "名著启蒙 逻辑思维",
            pageCount: 32,
            playCount: 7.96,
            category: "数学绘本", // 关联分类
            pages: [
                "/images/picturebook/rabbit_friend/page_rabbit_friend_1.jpg",
                "/images/picturebook/rabbit_friend/page_rabbit_friend_2.jpg",
                "/images/picturebook/rabbit_friend/page_rabbit_friend_3.jpg",
                "/images/picturebook/rabbit_friend/page_rabbit_friend_4.jpg",
                // ...更多页
            ]
        },
      ],
      filteredBookList: [] //筛选后的绘本列表（用于渲染右侧）
    },
  
    onLoad() {
        //初始化：默认筛选"经典绘本"的内容
        this.filterBooksByCategory("经典绘本");
        // 若需要从后端获取数据，可保留原wx.request逻辑，获取后再执行筛选
        // this.getBookListFromBackend();
    },

    //分类切换事件（点击左侧分类时触发）
    handleCategoryTap(e) {
        const selectedCategory = e.currentTarget.dataset.category;
        this.setData({
            activeCategory: selectedCategory // 更新选中状态
        });
        this.filterBooksByCategory(selectedCategory); // 筛选对应分类的绘本
    },

     //根据分类筛选绘本的核心方法
     filterBooksByCategory(category) {
        const filteredList = this.data.bookList.filter(book => book.category === category);
        
        this.setData({
            filteredBookList: filteredList // 更新筛选列表，触发页面渲染
        });
    },

    // （可选）从后端获取数据的方法（若需要对接真实接口）
    // getBookListFromBackend() {
    //     wx.request({
    //         url: 'https://your-backend-api/book/list',
    //         method: 'GET',
    //         success: (res) => {
    //             this.setData({
    //                 bookList: res.data // 假设后端返回包含category字段的绘本数组
    //             });
    //             // 后端数据获取后，默认筛选"经典绘本"
    //             this.filterBooksByCategory("经典绘本");
    //         }
    //     });
    // }












  })
  